<?php

/*
 * This class is used for as Model for Loan table 
 */

class Model_Installment extends Model_Base {

    protected $_name = 'tbl_installment';
    protected $_primaryKey = 'ins_id';

    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    //get customer data
    public function getcustomerArray($where ) {
        $customerModel = new Model_Customer();
        $customerArray = $customerModel->fetchCustomer($where, "", "0", 'cus_name', 'ASC');
       
        $finalArray[''] = 'Select Customer';
        if($where!=''){
            foreach ($customerArray as $com) {
                $finalArray[$com['cus_id']] = $com['cus_name'] . " #" . $com['cus_actnumber'];
            }
        }
        return $finalArray;
    }

    //get Agent data
    public function getloanArray( $where = "") {
        $loanModel = new Model_Loan();
        $loanArray = $loanModel->fetchLoan($where, "", "0", 'ln_id', 'ASC');
        
        $finalArray[''] = 'Select Loan';
        foreach ($loanArray as $com) {
            $finalArray[$com['ln_id']] = $com['ln_name'];
        }
        return $finalArray;
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('ins' => 'tbl_installment'))
                ->join(array('ln' => 'tbl_loan'), "ins.ins_ln_id = ln.ln_id")
                ->join(array('cus' => 'tbl_customer'), "cus.cus_id = ins.ins_cus_id")
                ->where('ins_id = ?', $id)
                ->where('ins.ins_status != 2');
        $data = $this->fetchRow($select);
        #echo $select;exit;
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('ln' => 'tbl_installment'))
                //->setIntegrityCheck(false)
                //->join(array('doc' => 'tbl_document'), "doc.doc_ln_id = ln.ln_id")
                ->where('ins_id = ?', $id)
                ->where('ins_status != 2');

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get loans
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchInstallment($where = "", $limit = "", $offset = "0", $order = 'ln_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('ins' => 'tbl_installment'))
                ->setIntegrityCheck(false)
                ->join(array('ln' => 'tbl_loan'), "ln.ln_id = ins.ins_ln_id")
                ->join(array('cus' => 'tbl_customer'), "cus.cus_id = ins.ins_cus_id")
                ->where('ins_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getInstallmentCount($where) {
        $select = $this->select()
                ->from(array('ins' => 'tbl_installment'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('ln' => 'tbl_loan'), "ln.ln_id = ins.ins_ln_id")
                ->join(array('cus' => 'tbl_customer'), "cus.cus_id = ins.ins_cus_id")
                ->where('ins_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete loans
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteInstallment($prdtArray) {
        $loginModel = new Model_Login();
        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_loan) {
                $loanData = Array();
                $loanData = $this->fetchEntryById($_loan);
                if (count($loanData) > 0) {
                    $data = array('ins_status' => '2');
                    $this->update($data, 'ins_id =' . (int) $_loan);
                }
            }
        } else {
            $ln_id = $prdtArray;
            $loanData = $this->fetchEntryById($ln_id);
            if (count($loanData) > 0) {
                $data = array('ins_status' => '2');
                $this->update($data, 'ins_id =' . (int) $ln_id);
            }
        }
    }

}
